var a,b,c;
require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#adDetails",
    data:{
        isNav:2,
        isNav1:2,
        isVideo:true,
        isVideo1:true,
        loadFlag:false,
        id:"",
        enterpriseAdvById:{},//视频信息
        createDate:"",
        uid:"",
        loginUid:"",
        pageNumber:"1",
        praiseState:"",//点赞状态
        praiseCount:"",//点赞人数
        accessKeyId:"",
        accessKeySecret:"",
        securityToken:"",
        release:{},
        type:"",
        sourceType:"1"
    },
    watch:{
        // getCanvas:function(){}
        // curr:function(){
        //     if(this.curr<this.vLen){
        //         vm.totalTime=0;
        //     }
        // }
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.id=this.searchObject&&this.searchObject.id;
        // this.accessKeyId=this.searchObject&&this.searchObject.accessKeyId;
        // this.accessKeySecret=this.searchObject&&this.searchObject.accessKeySecret;
        // this.securityToken=this.searchObject&&this.searchObject.securityToken;
        this.loginUid=this.searchObject&&this.searchObject.loginUid;
        this.type=this.searchObject&&this.searchObject.type;   //1 企业广告   2.自媒体视频  3城市视频
        //this.loginUid=this.searchObject&&this.searchObject.uid;
        // if(this.searchObject&&this.searchObject.loginUid){
        //     this.loginUid=this.searchObject&&this.searchObject.loginUid;
        // }else{
        //     if(!userInfo){
        //         location.href="/loginIndex"
        //     }else{
        //         this.loginUid=userInfo.loginUid;
        //     }
        // }
        this.releaseConfig();
        //this.getEnterpriseAdvById();
    },
    methods:{
        //后退
        getBack:function(){
            //location.href="/see/enterpriseAd";
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //通过用户id 视频id state点赞状态 1点赞0取消点赞
        getPraise:function(){
            var that=this,url;
            var data={
                loginUid:that.loginUid,
                id:that.id
                // state:that.praiseState
            };
            if(that.praiseState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            if(that.type==1){   //企业广告
                url="/see/getPraise";
            }else if(that.type==2){  //自媒体
                url="/see/getPraise";
            }else if(that.type==3){   //城市
                url="/cityAd/getCityAdvMediaInfo";
            }
            console.log(data);
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.praiseCount=b.json.data;
                    if(that.praiseState==0){
                        that.praiseState=1;
                    }else{
                        that.praiseState=0;
                    }
                    // console.log(that.praiseCount)
                }else{
                    vm.$refs.prompt.toast("点赞失败！")
                }
            });
        },
        getIsScreen:function(type){
            this.isScreen=type;
            if(type){
                this.continuity();
            }
        },
        //获取播放视频信息
        getEnterpriseAdvById:function(id){
            var that=this,data,url;
            if(id) {
                player.dispose();
                $(".adDetails-video").append("<div id='my-video'></div>");
                that.id = id;
                clearInterval(a);
                // myPlayer.pause();
            }
            data={
                id:that.id,
                loginUid:that.loginUid
            };
            if(that.type==1){   //企业广告
                url="/see/getEnterpriseAdvById";
            }else if(that.type==2){  //自媒体
                data.sourceType=that.sourceType;
                url="/selfMedia/getMediaAdvAdvById";
            }else if(that.type==3){   //城市
                url="/cityAd/getCityAdvMediaInfo";
            }
            // console.log(myPlayer.src)
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseAdvById=b.json.data;
                    that.uid=that.enterpriseAdvById.uid;
                    that.praiseState=that.enterpriseAdvById.praiseState;
                    that.praiseCount=that.enterpriseAdvById.praiseCount;
                    that.collectionState=that.enterpriseAdvById.collectionState;
                    // that.historyId=that.enterpriseAdvById.historyId;
                    // that.createDate=that.enterpriseAdvById.createDate.split(" ")[0].split("-");
                    // that.createDate=that.createDate[0]+"年"+that.createDate[1]+"月"+that.createDate[2]+"日";
                    // that.vList.push({"imgSrc":that.enterpriseAdvById.imgSrc,"id":that.enterpriseAdvById.id});
                    // that.enterpriseAdvById.recommends.forEach(function(item,index){
                    //     that.vList.push({"imgSrc":item.coverImgSrc,"id":item.id});
                    // });
                    // that.vLen=that.vList.length;
                    getPlayer(that.enterpriseAdvById.imgSrc);
                    // player.play();
                    if(that.type==1&&that.enterpriseAdvById&&that.enterpriseAdvById.officiaVideo){
                        getPlayer1(that.enterpriseAdvById.officiaVideo);
                    }
                }else{
                    vm.$refs.prompt.toast(b.json.msg||"获取播放视频信息失败！");
                }
            });
        },
        //获取配置文件
        releaseConfig:function(){
            var that=this,data;
            data={
                loginUid:that.loginUid
            };
            console.log(data)
            this.ajax({url:"/release/releaseConfig",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.release=b.json.data;
                    console.log(that.release)
                    that.getEnterpriseAdvById();
                }else{
                    vm.$refs.prompt.toast("获取视频信息失败！")
                }
            });
        },
        //显示视频等图标
        getIsVideo:function(type){
            if(type==1){
                this.isVideo=true;
                // b=setInterval(this.hideTip, 5000);
            }else if(type==2){
                this.isVideo1=true;
                // c=setInterval(this.hideTip1, 5000);
            }
        },
        hideTip:function(){
            this.isVideo=false;
            clearInterval(b);
        },
        hideTip1:function(){
            this.isVideo1=false;
            clearInterval(c);
        },
        //视频暂停
        getVideoPause:function(type){
            if(type==1){
                this.isNav=2;
                clearInterval(a);
                player.pause();
            }else if(type==2){
                this.isNav1=2;
                player1.pause();
            }
        },
        //视频播放
        getVideoPlay:function(type){
            if(type==1){
                this.isVideo=!this.isVideo;
                this.isNav=1;
                player.play();
            }else if(type==2){
                this.isVideo1=!this.isVideo1;
                this.isNav1=1;
                player1.play();
            }
        },
        getNext:function(){
            var u = navigator.userAgent;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
            if(isAndroid){
                window.open("http://zhushou.360.cn/detail/index/soft_id/4022099");
            }else if(isiOS){
                alert("ios")
            }
        }
    }
});
var player,player1;
function getPlayer(imgSrc){
    player = new Aliplayer({
        id: "my-video",
        "width":"100%",
        "height":"100%",
        //播放方式四：使用STS方式播放
        "vid": imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        "accessKeyId": vm.release.accessKeyId,  //vm.release.accessKeyId,  //
        "accessKeySecret": vm.release.accessKeySecret,//vm.release.securityToken,  //
        "securityToken": vm.release.securityToken,//vm.release.accessKeySecret   //
        "autoplay": false,
        "isLive": false,
        "rePlay": false,
        "playsinline": true,
        "preload": true,
        "controlBarVisibility": "click",
        "showBarTime": 0,
        "useH5Prism": true,
        "x5_type":'h5', //通过 video 属性 “x5-video-player-type” 声明启用同层H5播放器，支持的值：h5 https://x5.tencent.com/tbs/guide/video.html
        "x5_fullscreen":false,//通过 video 属性 “x5-video-player-fullscreen” 声明视频播放时是否进入到 TBS 的全屏模式，支持的值：true
        // "x5_video_position":"top",
        // "x5_orientation":"portraint",
        "skinLayout": [
            {
                "name": "controlBar",
                "align": "blabs",
                "x": 0,
                "y": 0,
                "children": [
                    {
                        "name": "progress",
                        "align": "blabs",
                        "x": 45,
                        "y": 15
                    },
                    {
                        "name": "timeDisplay",
                        "align": "tl",
                        "x": 7,
                        "y": 9
                    },
                    {
                        "name": "fullScreenButton",
                        "align": "tr",
                        "x": 8,
                        "y": 15
                    }
                ]
            }
        ]
    },function(player){
        console.log('播放器创建好了。')
    });
}
function getPlayer1(imgSrc){
    player1 = new Aliplayer({
        id: "my-video1",
        width:"100%",
        height:"100%",
        //播放方式四：使用STS方式播放
        vid : imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        autoplay:false,
        "accessKeyId": vm.release.accessKeyId,  //vm.release.accessKeyId,  //
        "accessKeySecret": vm.release.accessKeySecret,//vm.release.securityToken,  //
        "securityToken": vm.release.securityToken,//vm.release.accessKeySecret   //
    },function(player1){
        console.log('播放器创建好了。')
    });
}
function lazyload(){
    $("img.lazy").lazyload();
}